﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Threading;
using System.Windows.Forms;
using CurrencyConv.Core;
using CurrencyConv.Core.RatesDownloader;
using CurrencyConv.Core.SettingsManager;

namespace CurrencyConv
{
	public partial class Form1 : Form
	{
		private readonly IWebUpdater _webUpdate = IoC.Resolve<IWebUpdater>();
		private readonly IContextPersister _contextPersister = IoC.Resolve<IContextPersister>();
		private readonly ISettingsManager _settingsManager = IoC.Resolve<ISettingsManager>();

		private CurrencyCalculator _calculator;

		private readonly CCSettings _settings;
		private CurrencyConvContext _context;
		readonly IList<BoxAndCurrency> _availableCurrencyControls = new List<BoxAndCurrency>();
		private BoxAndCurrency _lastAccessedCurrencyControl = null;


		public Form1()
		{
			InitializeComponent();
			_settings = _settingsManager.LoadSettings();
		}

		private void Form1_Load(object sender, EventArgs e)
		{
			InitializeCombox();
			_availableCurrencyControls.Add(boxAndCurrency1);
			_availableCurrencyControls.Add(boxAndCurrency2);
			_availableCurrencyControls.Add(boxAndCurrency3);

			boxAndCurrency1.AmmountOrCurrencyChanged += new EventHandler(boxAndCurrency1_AmmountOrCurrencyChanged);
			boxAndCurrency2.AmmountOrCurrencyChanged += new EventHandler(boxAndCurrency1_AmmountOrCurrencyChanged);
			boxAndCurrency3.AmmountOrCurrencyChanged += new EventHandler(boxAndCurrency1_AmmountOrCurrencyChanged);

			boxAndCurrency1.TextboxGotFocus += new EventHandler(AmmountBoxGotFocus);
			boxAndCurrency2.TextboxGotFocus += new EventHandler(AmmountBoxGotFocus);
			boxAndCurrency3.TextboxGotFocus += new EventHandler(AmmountBoxGotFocus);

			inputPanel.EnabledChanged += new EventHandler(inputPanel_EnabledChanged);
		}

		#region move button down/up depending on PPC keyboard visible or not.

		void inputPanel_EnabledChanged(object sender, EventArgs e)
		{
			int y;

			if (inputPanel.Enabled)
				y = Height - inputPanel.Bounds.Height;
			else
				y = Height;

			y = y - btnMakeConversion.Height;
			int x = Width / 2 - btnMakeConversion.Width / 2;
			btnMakeConversion.Location = new Point(x, y);
		}

		#endregion

		private void AmmountBoxGotFocus(object sender, EventArgs e)
		{
			inputPanel.Enabled = true;
		}

		void boxAndCurrency1_AmmountOrCurrencyChanged(object sender, EventArgs e)
		{
			_lastAccessedCurrencyControl = sender as BoxAndCurrency;
		}

		private void InitializeCombox()
		{
			enumLoadRatesStatus status;
			_context = _contextPersister.LoadRates(out status);
			if (status != enumLoadRatesStatus.OK)
			{
				_context = _webUpdate.UpdateRatesFromWeb();

				if (_context == null)
				{
					MessageBox.Show("Could not get rates from : " + _settings.RemoteSiteAddress + ".\r\nMake sure you can access the internet.");
					return;
				}

				_contextPersister.SaveRates(_context);

				//TODO show info message.
			}

			_calculator = new CurrencyCalculator(_context.AvailableExchangeRates);

			BindBoxAndCurrency(boxAndCurrency1, _context.AvailableCurrencies, "RON");
			BindBoxAndCurrency(boxAndCurrency2, _context.AvailableCurrencies, "EUR");
			BindBoxAndCurrency(boxAndCurrency3, _context.AvailableCurrencies, "HRK");
		}

		private void BindBoxAndCurrency(BoxAndCurrency box, IEnumerable<Money> currencies, string selectedCurrency)
		{
			box.Bind(currencies, selectedCurrency);
		}

		private void btnMakeConversion_Click(object sender, EventArgs e)
		{
			Money currencyToConvertFrom = _lastAccessedCurrencyControl.SelectedCurrency;
			IEnumerable<BoxAndCurrency> otherCurrencies = _availableCurrencyControls.Where(cc => cc.SelectedCurrency != currencyToConvertFrom);

			foreach (BoxAndCurrency currency in otherCurrencies)
			{
				Money converted = _calculator.Convert(_lastAccessedCurrencyControl.Ammount, currency.SelectedCurrency);
				currency.Ammount = converted;
			}
		}

		private void Exit_Click(object sender, EventArgs e)
		{
			DialogResult result = MessageBox.Show("Exit ?", "Exit ?", MessageBoxButtons.OKCancel, MessageBoxIcon.Question,
																						MessageBoxDefaultButton.Button1);

			if (result == DialogResult.OK)
				Application.Exit();
		}
	}
}
